package com.tencent.wxcloudrun.task;

import com.alibaba.fastjson2.JSONObject;
import com.tencent.wxcloudrun.dao.MyRemindMapper;
import com.tencent.wxcloudrun.model.MyRemind;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Component
public class RemindTask {
    @Autowired
    private MyRemindMapper remindMapper;



    @Scheduled(cron = "0 0/5 * * * ?")
    public void remind() {

//        QueryWrapper<MyRemind> goodsQueryWrapper = new QueryWrapper<>();
//        goodsQueryWrapper.eq("enable", 1);
//        goodsQueryWrapper.gt()
        List<MyRemind> remindPushMsg = remindMapper.findRemindPushMsg();

        remindPushMsg.forEach(a->{
            System.out.println(a.getBusiTime());
            callWxPush(a);
        });

        System.out.println("remind");
    }

    public void callWxPush(MyRemind remind){
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        String pushUrl = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=ACCESS_TOKEN";
        String pushUrl1 = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send";


//        HttpPost httpPost = new HttpPost(pushUrl);
//        HttpEntity entity1=new BasicHttpEntity();
//        httpPost.setEntity(null);

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("touser",remind.getUserId());
        jsonBody.put("template_id","mtnD926xUPFces8NoQqxh5aZC5RixWK6_oa4l12fwjQ");
        JSONObject temp = JSONObject.parseObject("{\"thing1\":{\"value\":\""+remind.getTitle()+"\"},\"thing2\":{\"value\":\""+remind.getDescription()+"\"}}");
        jsonBody.put("data",temp);
        jsonBody.put("miniprogram_state","developer");
        jsonBody.put("lang","zh_CN");
        System.out.println(jsonBody.toString());
        org.springframework.http.HttpEntity<String> request = new org.springframework.http.HttpEntity<>(jsonBody.toString(), headers);
        String post = restTemplate.postForObject(pushUrl1, request, String.class);
        System.out.println(post);


        /*try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
            // 处理 response
            HttpEntity entity = response.getEntity();
            String body = "";
            if (entity != null) {
//                try (InputStream inputStream = entity.getContent()) {
//                    body = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
//                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }*/
    }
}
